﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace LunwenYun.Infrastructure.Entities.Repository
{
    [Table("T_MessageBoard")]
    public class T_MessageBoard : IEntity<T_MessageBoard>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 1：论文 2：投稿意向
        /// </summary>
        public int To { get; set; }
        /// <summary>
        /// 1：留言 2：私信
        /// </summary>
        public int MessageType { get; set; }
        /// <summary>
        /// 1：ArticleId
        /// </summary>
        public int RelateId { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(512)]
        public string Message { get; set; }

        public DateTime CreateDate { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string From { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string FromName { get; set; }



        public override T_MessageBoard Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_MessageBoard Find(System.Linq.Expressions.Expression<Func<T_MessageBoard, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.MessageBoard.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateDate = item.CreateDate;
                    this.From = item.From;
                    this.FromName = item.FromName;
                    this.Id = item.Id;
                    this.Message = item.Message;
                    this.MessageType = item.MessageType;
                    this.RelateId = item.RelateId;
                    this.To = item.To;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_MessageBoard, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.MessageBoard.Any(where);
            }
        }
    }
}
